package com.ttbj.core.dao.dataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 数据库连接线程
 * 
 * @author Daniel
 * @date 下午6:08:33
 * 
 */
public class DataBaseContextHolder {

	private static Logger logger = LoggerFactory.getLogger(DataBaseContextHolder.class);

	private static ThreadLocal<String> contextHolder = new ThreadLocal<String>();

	public static final String DEFAULT_TARGET_DATASOURCE = "defaultTargetDataSource";

	/**
	 * 获取本线程的databaseType
	 * 
	 * @author Daniel
	 * @date 下午6:27:42
	 * 
	 * @return
	 */
	public static String getDatabaseType() {
		String databaseType = contextHolder.get();
		if (databaseType == null) {
			return DEFAULT_TARGET_DATASOURCE;
		}
		return databaseType;
	}

	/**
	 * 设置本线程的databaseType
	 * 
	 * @author Daniel
	 * @date 下午6:29:40
	 * 
	 * @param databaseType
	 */
	public static void setDatabaseType(String databaseType) {
		if (databaseType == null) {
			contextHolder.set(DEFAULT_TARGET_DATASOURCE);
		}
		contextHolder.set(databaseType);
	}

	/**
	 * 
	 * 清理数据库连接
	 * 
	 * @author Daniel
	 * @date 下午6:29:36
	 * 
	 */
	public static void cleanDatabaseType() {
		contextHolder.remove();
	}

}
